/*
 * Package il.ac.biu.cs.grossmm.impl.activeData
 * File SqlEventInspector.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.impl.activeData;

import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.data.*;
import il.ac.biu.cs.grossmm.api.flow.NodeEventInspector;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

public class SqlEventInspector extends SqlInspector implements
		NodeEventInspector {

	final Map<SqlNodeBase, SqlNodeModification> modificationMap;

	public SqlEventInspector(SqlManipulator man) {
		super();
		this.modificationMap = man.modificationMap;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <N, P> P getValue(Node<N> node, Property<N, P> property)
			throws OperationFailedException {
		SqlNodeModification modification = modificationMap.get(node);

		if (modification == null) {
			// node was not modified
			return super.getValue(node, property);
		} else {
			Object[] values = modification.newValues;
			if (values == null) {
				// values were not modified
				return super.getValue(node, property);
			} else {
				// return modified values
				return (P) values[property.index()];
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <N, S> Set<Node<S>> getSubnodes(Node<N> node, Nodeset<N, S> nodeSet)
			throws OperationFailedException {
		SqlNodeModification modification = modificationMap.get(node);

		if (modification == null) {
			// node was not modified
			return super.getSubnodes(node, nodeSet);
		} else {
			int index = nodeSet.index();
			SqlNodesetModification nm = modification.nodesets[index];

			if (nm == null) {
				// nodes in this nodeset were not added/removed
				return super.getSubnodes(node, nodeSet);
			} else {
				// return modified values
				return (Set) nm.getMerged();
			}
		}
	}

	@SuppressWarnings("unchecked")
	public <N, V> V getOldValue(Node<N> node, Property<N, V> property)
			throws OperationFailedException {
		SqlNodeModification modification = modificationMap.get(node);

		if (modification == null) {
			// node was not modified
			return super.getValue(node, property);
		} else {
			Object[] values = modification.oldValues;
			if (values == null) {
				// values were not modified
				return super.getValue(node, property);
			} else {
				// return modified values
				int index = property.index();
				return (V) values[index];
			}
		}
	}

	@SuppressWarnings("unchecked")
	public <N, S> Set<Node<S>> getSubnodes(Node<N> node, Nodeset<N, S> nodeSet,
			UpdateType u) throws OperationFailedException {
		SqlNodeModification modification = modificationMap.get(node);

		if (modification == null) {
			// node was not modified
			if (u == UpdateType.UNCHANGED)
				// none of subnodes were modified - return all
				return super.getSubnodes(node, nodeSet);
			else
				// none of subnodes were modified - return nothing
				return Collections.EMPTY_SET;
		} else {
			int index = nodeSet.index();
			SqlNodesetModification nm = modification.nodesets[index];

			if (nm == null) {
				if (u == UpdateType.UNCHANGED)
					// none of subnodes were modified - return all
					return super.getSubnodes(node, nodeSet);
				else
					// none of subnodes were modified - return nothing
					return Collections.EMPTY_SET;
			} else {
				// return corresponding set
				return (Set) nm.children[u.ordinal()];
			}
		}
	}

	public <N> UpdateType getUpdateType(Node<N> node) {
		SqlNodeModification modification = modificationMap.get(node);

		if (modification == null)
			return UpdateType.UNCHANGED;
		else
			return modification.updateType;
	}

}
